/*************************************************
utils.cpp
Author:Mitom
Date:2019-5-30
Description:工具
**************************************************/

#include "sEMG.hpp"
#include "utils.hpp"

//获取文件行数
int getFileRowCount(string file)
{
	ifstream infile;
	infile.open(file.data());
	//assert(infile.is_open());
	int rowNumber = 0;
	string s;
	while(getline(infile,s)){
		rowNumber++;
	}
	infile.close();
	return rowNumber;
}

//获取文件列数
int getFileColCount(string file)
{
	ifstream infile;
	infile.open(file.data());
	//assert(infile.is_open());
	int colNumber = 0;
	string s;
	vector<string> vecRow;
	while(getline(infile,s)){
		vecRow = split(s, " ");
		break;
	}

	colNumber = vecRow.size();
	infile.close();

	return colNumber;
}

//字符串分割函数
vector<string> split(string srcStr, const string& delim)
{
	int nPos = 0;
	vector<string> vec;
	nPos = srcStr.find(delim.c_str());
	while(-1 != nPos)
	{
		string temp = srcStr.substr(0, nPos);
		if(temp!="" && temp!=" "){
			vec.push_back(temp);
		}
		srcStr = srcStr.substr(nPos+1);
		nPos = srcStr.find(delim.c_str());
	}
	if(srcStr!="" && srcStr!=" "){
		vec.push_back(srcStr);
	}
	return vec;
}

//加载模型参数
Matrix loadParameter(string file)
{
	ifstream infile;
	infile.open(file.data());
	//assert(infile.is_open());
	int rowCount = getFileRowCount(file);
	int colCount = getFileColCount(file);

	Matrix mat = Matrix(rowCount, colCount);
	double **matPtr = mat.getPtr();

    string s;
    int rowIndex = 0;
    while(getline(infile,s))
    {
        vector<string> ret = split(s, " ");
        for(size_t i=0; i<ret.size(); i++){
        	//atof报错
        	//matPtr[rowIndex][i] = atof(ret[i].c_str());
        }
        rowIndex++;
    }

    infile.close();

    return mat;
}


